package cn.itcast.web.task;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.cxf.jaxrs.client.WebClient;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 定时查询保运结果
 *    时间：每5秒查询
 */
public class ExportTask {

    @Reference
    private ExportService exportService;

    public void execute() {
        //1、查询所有状态=2的报运单（没有保运结果的报运单）
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(2l);
        List<Export> list = exportService.findAll(example);
        //2、循环报运单
        for (Export export : list) {
            //3、通过webclient发送请求获取响应
            WebClient webClient = WebClient.create("http://localhost:9001/ws/export/ep/" + export.getId());
            ExportResult result = webClient.get(ExportResult.class);
            //4、判断返回状态 （需要处理状态!=2的报运单）
            if(result.getState() != 2) {
                //5、调用service处理报运结果
                exportService.updateE(result);
            }
        }
    }

}
